library(dplyr)
library(magrittr)
library(sqldf)
library(tidyverse)
library(optparse)

option_list <- list(
  make_option(c("-i", "--inputfile"), default = "*", type = "character", help = "inputfile path"),
  make_option(c("-o", "--outputfile"), default = "sample_color.txt", type = "character", help = "inputfile path")
)
opt <- parse_args(OptionParser(option_list = option_list))

df <- read.delim(opt$inputfile, check.names = FALSE, header = FALSE)

checkname <- function(x) {
  temp <- unlist(strsplit(as.character(x), "\\."))
  return(temp[length(temp)])
}

df <- df$V1 %>%
  sapply(function(x) checkname(x)) %>%
  data.frame() %>%
  cbind(df, .)


colnames(df) <- c("ida1", "ida2", "ida3", "ida4", "ida5", "ida6")

df %<>% filter(ida6 != "Other", ida6 != "norank", ida6 != "uncultured")
df <- df[, -c(6)]
write.table(df, file = opt$inputfile, sep = "\t", col.names = FALSE, row.names = FALSE, na = "", quote = FALSE)

df %<>% subset(ida3 != "")
write.table(df, file = opt$outputfile, sep = "\t", col.names = FALSE, row.names = FALSE, quote = FALSE)